package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.CurePigDAO;
import cn.edu.scau.cmi.hombio.dao.PigDAO;
import cn.edu.scau.cmi.hombio.dao.PigTypeDAO;
import cn.edu.scau.cmi.hombio.dao.PigWeightDAO;
import cn.edu.scau.cmi.hombio.dao.SalePigDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPigDAO;
import cn.edu.scau.cmi.hombio.dao.TransferDAO;

import cn.edu.scau.cmi.hombio.domain.CurePig;
import cn.edu.scau.cmi.hombio.domain.Pig;
import cn.edu.scau.cmi.hombio.domain.PigType;
import cn.edu.scau.cmi.hombio.domain.PigWeight;
import cn.edu.scau.cmi.hombio.domain.SalePig;
import cn.edu.scau.cmi.hombio.domain.TaskPig;
import cn.edu.scau.cmi.hombio.domain.Transfer;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Pig entities
 * 
 */

@Service("PigService")

@Transactional
public class PigServiceImpl implements PigService {

	/**
	 * DAO injected by Spring that manages CurePig entities
	 * 
	 */
	@Autowired
	private CurePigDAO curePigDAO;

	/**
	 * DAO injected by Spring that manages Pig entities
	 * 
	 */
	@Autowired
	private PigDAO pigDAO;

	/**
	 * DAO injected by Spring that manages PigType entities
	 * 
	 */
	@Autowired
	private PigTypeDAO pigTypeDAO;

	/**
	 * DAO injected by Spring that manages PigWeight entities
	 * 
	 */
	@Autowired
	private PigWeightDAO pigWeightDAO;

	/**
	 * DAO injected by Spring that manages SalePig entities
	 * 
	 */
	@Autowired
	private SalePigDAO salePigDAO;

	/**
	 * DAO injected by Spring that manages TaskPig entities
	 * 
	 */
	@Autowired
	private TaskPigDAO taskPigDAO;

	/**
	 * DAO injected by Spring that manages Transfer entities
	 * 
	 */
	@Autowired
	private TransferDAO transferDAO;

	/**
	 * Instantiates a new PigServiceImpl.
	 *
	 */
	public PigServiceImpl() {
	}

	/**
	 * Delete an existing CurePig entity
	 * 
	 */
	@Transactional
	public Pig deletePigCurePigs(Integer pig_id, Integer related_curepigs_id) {
		CurePig related_curepigs = curePigDAO.findCurePigByPrimaryKey(related_curepigs_id, -1, -1);

		Pig pig = pigDAO.findPigByPrimaryKey(pig_id, -1, -1);

		related_curepigs.setPig(null);
		pig.getCurePigs().remove(related_curepigs);

		curePigDAO.remove(related_curepigs);
		curePigDAO.flush();

		return pig;
	}

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig deletePigPigByThirdLikelyMaleParentId(Integer pig_id, Integer related_pigbythirdlikelymaleparentid_id) {
		Pig pig = pigDAO.findPigByPrimaryKey(pig_id, -1, -1);
		Pig related_pigbythirdlikelymaleparentid = pigDAO.findPigByPrimaryKey(related_pigbythirdlikelymaleparentid_id, -1, -1);

		pig.setPigByThirdLikelyMaleParentId(null);
		related_pigbythirdlikelymaleparentid.getPigsForThirdLikelyMaleParentId().remove(pig);
		pig = pigDAO.store(pig);
		pigDAO.flush();

		related_pigbythirdlikelymaleparentid = pigDAO.store(related_pigbythirdlikelymaleparentid);
		pigDAO.flush();

		pigDAO.remove(related_pigbythirdlikelymaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Delete an existing PigType entity
	 * 
	 */
	@Transactional
	public Pig deletePigPigType(Integer pig_id, Integer related_pigtype_id) {
		Pig pig = pigDAO.findPigByPrimaryKey(pig_id, -1, -1);
		PigType related_pigtype = pigTypeDAO.findPigTypeByPrimaryKey(related_pigtype_id, -1, -1);

		pig.setPigType(null);
		related_pigtype.getPigs().remove(pig);
		pig = pigDAO.store(pig);
		pigDAO.flush();

		related_pigtype = pigTypeDAO.store(related_pigtype);
		pigTypeDAO.flush();

		pigTypeDAO.remove(related_pigtype);
		pigTypeDAO.flush();

		return pig;
	}

	/**
	 * Save an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig savePigPigByFemaleParentId(Integer id, Pig related_pigbyfemaleparentid) {
		Pig pig = pigDAO.findPigByPrimaryKey(id, -1, -1);
		Pig existingpigByFemaleParentId = pigDAO.findPigByPrimaryKey(related_pigbyfemaleparentid.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigByFemaleParentId != null) {
			existingpigByFemaleParentId.setId(related_pigbyfemaleparentid.getId());
			existingpigByFemaleParentId.setNumber(related_pigbyfemaleparentid.getNumber());
			existingpigByFemaleParentId.setEarNumber(related_pigbyfemaleparentid.getEarNumber());
			existingpigByFemaleParentId.setRfid(related_pigbyfemaleparentid.getRfid());
			existingpigByFemaleParentId.setBirthday(related_pigbyfemaleparentid.getBirthday());
			existingpigByFemaleParentId.setState(related_pigbyfemaleparentid.getState());
			existingpigByFemaleParentId.setMemo(related_pigbyfemaleparentid.getMemo());
			related_pigbyfemaleparentid = existingpigByFemaleParentId;
		} else {
			related_pigbyfemaleparentid = pigDAO.store(related_pigbyfemaleparentid);
			pigDAO.flush();
		}

		pig.setPigByFemaleParentId(related_pigbyfemaleparentid);
		related_pigbyfemaleparentid.getPigsForFemaleParentId().add(pig);
		pig = pigDAO.store(pig);
		pigDAO.flush();

		related_pigbyfemaleparentid = pigDAO.store(related_pigbyfemaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Save an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig savePigPigBySecondLikelyMaleParentId(Integer id, Pig related_pigbysecondlikelymaleparentid) {
		Pig pig = pigDAO.findPigByPrimaryKey(id, -1, -1);
		Pig existingpigBySecondLikelyMaleParentId = pigDAO.findPigByPrimaryKey(related_pigbysecondlikelymaleparentid.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigBySecondLikelyMaleParentId != null) {
			existingpigBySecondLikelyMaleParentId.setId(related_pigbysecondlikelymaleparentid.getId());
			existingpigBySecondLikelyMaleParentId.setNumber(related_pigbysecondlikelymaleparentid.getNumber());
			existingpigBySecondLikelyMaleParentId.setEarNumber(related_pigbysecondlikelymaleparentid.getEarNumber());
			existingpigBySecondLikelyMaleParentId.setRfid(related_pigbysecondlikelymaleparentid.getRfid());
			existingpigBySecondLikelyMaleParentId.setBirthday(related_pigbysecondlikelymaleparentid.getBirthday());
			existingpigBySecondLikelyMaleParentId.setState(related_pigbysecondlikelymaleparentid.getState());
			existingpigBySecondLikelyMaleParentId.setMemo(related_pigbysecondlikelymaleparentid.getMemo());
			related_pigbysecondlikelymaleparentid = existingpigBySecondLikelyMaleParentId;
		} else {
			related_pigbysecondlikelymaleparentid = pigDAO.store(related_pigbysecondlikelymaleparentid);
			pigDAO.flush();
		}

		pig.setPigBySecondLikelyMaleParentId(related_pigbysecondlikelymaleparentid);
		related_pigbysecondlikelymaleparentid.getPigsForSecondLikelyMaleParentId().add(pig);
		pig = pigDAO.store(pig);
		pigDAO.flush();

		related_pigbysecondlikelymaleparentid = pigDAO.store(related_pigbysecondlikelymaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 */
	@Transactional
	public Pig findPigByPrimaryKey(Integer id) {
		return pigDAO.findPigByPrimaryKey(id);
	}

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig deletePigPigsForMostLikelyMaleParentId(Integer pig_id, Integer related_pigsformostlikelymaleparentid_id) {
		Pig related_pigsformostlikelymaleparentid = pigDAO.findPigByPrimaryKey(related_pigsformostlikelymaleparentid_id, -1, -1);

		Pig pig = pigDAO.findPigByPrimaryKey(pig_id, -1, -1);

		pig.setPigByFemaleParentId(null);
		related_pigsformostlikelymaleparentid.getPigsForFemaleParentId().remove(pig);

		pigDAO.remove(related_pigsformostlikelymaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Load an existing Pig entity
	 * 
	 */
	@Transactional
	public Set<Pig> loadPigs() {
		return pigDAO.findAllPigs();
	}

	/**
	 * Delete an existing SalePig entity
	 * 
	 */
	@Transactional
	public Pig deletePigSalePigs(Integer pig_id, Integer related_salepigs_id) {
		SalePig related_salepigs = salePigDAO.findSalePigByPrimaryKey(related_salepigs_id, -1, -1);

		Pig pig = pigDAO.findPigByPrimaryKey(pig_id, -1, -1);

		related_salepigs.setPig(null);
		pig.getSalePigs().remove(related_salepigs);

		salePigDAO.remove(related_salepigs);
		salePigDAO.flush();

		return pig;
	}

	/**
	 * Save an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig savePigPigsForThirdLikelyMaleParentId(Integer id, Pig related_pigsforthirdlikelymaleparentid) {
		Pig pig = pigDAO.findPigByPrimaryKey(id, -1, -1);
		Pig existingpigsForThirdLikelyMaleParentId = pigDAO.findPigByPrimaryKey(related_pigsforthirdlikelymaleparentid.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigsForThirdLikelyMaleParentId != null) {
			existingpigsForThirdLikelyMaleParentId.setId(related_pigsforthirdlikelymaleparentid.getId());
			existingpigsForThirdLikelyMaleParentId.setNumber(related_pigsforthirdlikelymaleparentid.getNumber());
			existingpigsForThirdLikelyMaleParentId.setEarNumber(related_pigsforthirdlikelymaleparentid.getEarNumber());
			existingpigsForThirdLikelyMaleParentId.setRfid(related_pigsforthirdlikelymaleparentid.getRfid());
			existingpigsForThirdLikelyMaleParentId.setBirthday(related_pigsforthirdlikelymaleparentid.getBirthday());
			existingpigsForThirdLikelyMaleParentId.setState(related_pigsforthirdlikelymaleparentid.getState());
			existingpigsForThirdLikelyMaleParentId.setMemo(related_pigsforthirdlikelymaleparentid.getMemo());
			related_pigsforthirdlikelymaleparentid = existingpigsForThirdLikelyMaleParentId;
		} else {
			related_pigsforthirdlikelymaleparentid = pigDAO.store(related_pigsforthirdlikelymaleparentid);
			pigDAO.flush();
		}

		pig.setPigByFemaleParentId(related_pigsforthirdlikelymaleparentid);
		related_pigsforthirdlikelymaleparentid.getPigsForFemaleParentId().add(pig);
		pig = pigDAO.store(pig);
		pigDAO.flush();

		related_pigsforthirdlikelymaleparentid = pigDAO.store(related_pigsforthirdlikelymaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Return all Pig entity
	 * 
	 */
	@Transactional
	public List<Pig> findAllPigs(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Pig>(pigDAO.findAllPigs(startResult, maxRows));
	}

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@Transactional
	public void deletePig(Pig pig) {
		pigDAO.remove(pig);
		pigDAO.flush();
	}

	/**
	 * Delete an existing PigWeight entity
	 * 
	 */
	@Transactional
	public Pig deletePigPigWeights(Integer pig_id, Integer related_pigweights_id) {
		PigWeight related_pigweights = pigWeightDAO.findPigWeightByPrimaryKey(related_pigweights_id, -1, -1);

		Pig pig = pigDAO.findPigByPrimaryKey(pig_id, -1, -1);

		related_pigweights.setPig(null);
		pig.getPigWeights().remove(related_pigweights);

		pigWeightDAO.remove(related_pigweights);
		pigWeightDAO.flush();

		return pig;
	}

	/**
	 * Save an existing Pig entity
	 * 
	 */
	@Transactional
	public void savePig(Pig pig) {
		Pig existingPig = pigDAO.findPigByPrimaryKey(pig.getId());

		if (existingPig != null) {
			if (existingPig != pig) {
				existingPig.setId(pig.getId());
				existingPig.setNumber(pig.getNumber());
				existingPig.setEarNumber(pig.getEarNumber());
				existingPig.setRfid(pig.getRfid());
				existingPig.setBirthday(pig.getBirthday());
				existingPig.setState(pig.getState());
				existingPig.setMemo(pig.getMemo());
			}
			pig = pigDAO.store(existingPig);
		} else {
			pig = pigDAO.store(pig);
		}
		pigDAO.flush();
	}

	/**
	 * Save an existing SalePig entity
	 * 
	 */
	@Transactional
	public Pig savePigSalePigs(Integer id, SalePig related_salepigs) {
		Pig pig = pigDAO.findPigByPrimaryKey(id, -1, -1);
		SalePig existingsalePigs = salePigDAO.findSalePigByPrimaryKey(related_salepigs.getId());

		// copy into the existing record to preserve existing relationships
		if (existingsalePigs != null) {
			existingsalePigs.setId(related_salepigs.getId());
			existingsalePigs.setTime(related_salepigs.getTime());
			existingsalePigs.setMemo(related_salepigs.getMemo());
			related_salepigs = existingsalePigs;
		}

		related_salepigs.setPig(pig);
		pig.getSalePigs().add(related_salepigs);
		related_salepigs = salePigDAO.store(related_salepigs);
		salePigDAO.flush();

		pig = pigDAO.store(pig);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig deletePigPigsForFemaleParentId(Integer pig_id, Integer related_pigsforfemaleparentid_id) {
		Pig related_pigsforfemaleparentid = pigDAO.findPigByPrimaryKey(related_pigsforfemaleparentid_id, -1, -1);

		Pig pig = pigDAO.findPigByPrimaryKey(pig_id, -1, -1);

		pig.setPigByFemaleParentId(null);
		related_pigsforfemaleparentid.getPigsForFemaleParentId().remove(pig);

		pigDAO.remove(related_pigsforfemaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Delete an existing TaskPig entity
	 * 
	 */
	@Transactional
	public Pig deletePigTaskPigs(Integer pig_id, Integer related_taskpigs_id) {
		TaskPig related_taskpigs = taskPigDAO.findTaskPigByPrimaryKey(related_taskpigs_id, -1, -1);

		Pig pig = pigDAO.findPigByPrimaryKey(pig_id, -1, -1);

		related_taskpigs.setPig(null);
		pig.getTaskPigs().remove(related_taskpigs);

		taskPigDAO.remove(related_taskpigs);
		taskPigDAO.flush();

		return pig;
	}

	/**
	 * Return a count of all Pig entity
	 * 
	 */
	@Transactional
	public Integer countPigs() {
		return ((Long) pigDAO.createQuerySingleResult("select count(o) from Pig o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig deletePigPigByMostLikelyMaleParentId(Integer pig_id, Integer related_pigbymostlikelymaleparentid_id) {
		Pig pig = pigDAO.findPigByPrimaryKey(pig_id, -1, -1);
		Pig related_pigbymostlikelymaleparentid = pigDAO.findPigByPrimaryKey(related_pigbymostlikelymaleparentid_id, -1, -1);

		pig.setPigByMostLikelyMaleParentId(null);
		related_pigbymostlikelymaleparentid.getPigsForMostLikelyMaleParentId().remove(pig);
		pig = pigDAO.store(pig);
		pigDAO.flush();

		related_pigbymostlikelymaleparentid = pigDAO.store(related_pigbymostlikelymaleparentid);
		pigDAO.flush();

		pigDAO.remove(related_pigbymostlikelymaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Delete an existing Transfer entity
	 * 
	 */
	@Transactional
	public Pig deletePigTransfers(Integer pig_id, Integer related_transfers_id) {
		Transfer related_transfers = transferDAO.findTransferByPrimaryKey(related_transfers_id, -1, -1);

		Pig pig = pigDAO.findPigByPrimaryKey(pig_id, -1, -1);

		related_transfers.setPig(null);
		pig.getTransfers().remove(related_transfers);

		transferDAO.remove(related_transfers);
		transferDAO.flush();

		return pig;
	}

	/**
	 * Save an existing CurePig entity
	 * 
	 */
	@Transactional
	public Pig savePigCurePigs(Integer id, CurePig related_curepigs) {
		Pig pig = pigDAO.findPigByPrimaryKey(id, -1, -1);
		CurePig existingcurePigs = curePigDAO.findCurePigByPrimaryKey(related_curepigs.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcurePigs != null) {
			existingcurePigs.setId(related_curepigs.getId());
			existingcurePigs.setCureTime(related_curepigs.getCureTime());
			existingcurePigs.setMemo(related_curepigs.getMemo());
			related_curepigs = existingcurePigs;
		}

		related_curepigs.setPig(pig);
		pig.getCurePigs().add(related_curepigs);
		related_curepigs = curePigDAO.store(related_curepigs);
		curePigDAO.flush();

		pig = pigDAO.store(pig);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Save an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig savePigPigByThirdLikelyMaleParentId(Integer id, Pig related_pigbythirdlikelymaleparentid) {
		Pig pig = pigDAO.findPigByPrimaryKey(id, -1, -1);
		Pig existingpigByThirdLikelyMaleParentId = pigDAO.findPigByPrimaryKey(related_pigbythirdlikelymaleparentid.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigByThirdLikelyMaleParentId != null) {
			existingpigByThirdLikelyMaleParentId.setId(related_pigbythirdlikelymaleparentid.getId());
			existingpigByThirdLikelyMaleParentId.setNumber(related_pigbythirdlikelymaleparentid.getNumber());
			existingpigByThirdLikelyMaleParentId.setEarNumber(related_pigbythirdlikelymaleparentid.getEarNumber());
			existingpigByThirdLikelyMaleParentId.setRfid(related_pigbythirdlikelymaleparentid.getRfid());
			existingpigByThirdLikelyMaleParentId.setBirthday(related_pigbythirdlikelymaleparentid.getBirthday());
			existingpigByThirdLikelyMaleParentId.setState(related_pigbythirdlikelymaleparentid.getState());
			existingpigByThirdLikelyMaleParentId.setMemo(related_pigbythirdlikelymaleparentid.getMemo());
			related_pigbythirdlikelymaleparentid = existingpigByThirdLikelyMaleParentId;
		} else {
			related_pigbythirdlikelymaleparentid = pigDAO.store(related_pigbythirdlikelymaleparentid);
			pigDAO.flush();
		}

		pig.setPigByThirdLikelyMaleParentId(related_pigbythirdlikelymaleparentid);
		related_pigbythirdlikelymaleparentid.getPigsForThirdLikelyMaleParentId().add(pig);
		pig = pigDAO.store(pig);
		pigDAO.flush();

		related_pigbythirdlikelymaleparentid = pigDAO.store(related_pigbythirdlikelymaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Save an existing Transfer entity
	 * 
	 */
	@Transactional
	public Pig savePigTransfers(Integer id, Transfer related_transfers) {
		Pig pig = pigDAO.findPigByPrimaryKey(id, -1, -1);
		Transfer existingtransfers = transferDAO.findTransferByPrimaryKey(related_transfers.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtransfers != null) {
			existingtransfers.setId(related_transfers.getId());
			existingtransfers.setTime(related_transfers.getTime());
			existingtransfers.setMemo(related_transfers.getMemo());
			related_transfers = existingtransfers;
		}

		related_transfers.setPig(pig);
		pig.getTransfers().add(related_transfers);
		related_transfers = transferDAO.store(related_transfers);
		transferDAO.flush();

		pig = pigDAO.store(pig);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Save an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig savePigPigsForFemaleParentId(Integer id, Pig related_pigsforfemaleparentid) {
		Pig pig = pigDAO.findPigByPrimaryKey(id, -1, -1);
		Pig existingpigsForFemaleParentId = pigDAO.findPigByPrimaryKey(related_pigsforfemaleparentid.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigsForFemaleParentId != null) {
			existingpigsForFemaleParentId.setId(related_pigsforfemaleparentid.getId());
			existingpigsForFemaleParentId.setNumber(related_pigsforfemaleparentid.getNumber());
			existingpigsForFemaleParentId.setEarNumber(related_pigsforfemaleparentid.getEarNumber());
			existingpigsForFemaleParentId.setRfid(related_pigsforfemaleparentid.getRfid());
			existingpigsForFemaleParentId.setBirthday(related_pigsforfemaleparentid.getBirthday());
			existingpigsForFemaleParentId.setState(related_pigsforfemaleparentid.getState());
			existingpigsForFemaleParentId.setMemo(related_pigsforfemaleparentid.getMemo());
			related_pigsforfemaleparentid = existingpigsForFemaleParentId;
		} else {
			related_pigsforfemaleparentid = pigDAO.store(related_pigsforfemaleparentid);
			pigDAO.flush();
		}

		pig.setPigByFemaleParentId(related_pigsforfemaleparentid);
		related_pigsforfemaleparentid.getPigsForFemaleParentId().add(pig);
		pig = pigDAO.store(pig);
		pigDAO.flush();

		related_pigsforfemaleparentid = pigDAO.store(related_pigsforfemaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Save an existing TaskPig entity
	 * 
	 */
	@Transactional
	public Pig savePigTaskPigs(Integer id, TaskPig related_taskpigs) {
		Pig pig = pigDAO.findPigByPrimaryKey(id, -1, -1);
		TaskPig existingtaskPigs = taskPigDAO.findTaskPigByPrimaryKey(related_taskpigs.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskPigs != null) {
			existingtaskPigs.setId(related_taskpigs.getId());
			existingtaskPigs.setMemo(related_taskpigs.getMemo());
			related_taskpigs = existingtaskPigs;
		}

		related_taskpigs.setPig(pig);
		pig.getTaskPigs().add(related_taskpigs);
		related_taskpigs = taskPigDAO.store(related_taskpigs);
		taskPigDAO.flush();

		pig = pigDAO.store(pig);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Save an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig savePigPigsForSecondLikelyMaleParentId(Integer id, Pig related_pigsforsecondlikelymaleparentid) {
		Pig pig = pigDAO.findPigByPrimaryKey(id, -1, -1);
		Pig existingpigsForSecondLikelyMaleParentId = pigDAO.findPigByPrimaryKey(related_pigsforsecondlikelymaleparentid.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigsForSecondLikelyMaleParentId != null) {
			existingpigsForSecondLikelyMaleParentId.setId(related_pigsforsecondlikelymaleparentid.getId());
			existingpigsForSecondLikelyMaleParentId.setNumber(related_pigsforsecondlikelymaleparentid.getNumber());
			existingpigsForSecondLikelyMaleParentId.setEarNumber(related_pigsforsecondlikelymaleparentid.getEarNumber());
			existingpigsForSecondLikelyMaleParentId.setRfid(related_pigsforsecondlikelymaleparentid.getRfid());
			existingpigsForSecondLikelyMaleParentId.setBirthday(related_pigsforsecondlikelymaleparentid.getBirthday());
			existingpigsForSecondLikelyMaleParentId.setState(related_pigsforsecondlikelymaleparentid.getState());
			existingpigsForSecondLikelyMaleParentId.setMemo(related_pigsforsecondlikelymaleparentid.getMemo());
			related_pigsforsecondlikelymaleparentid = existingpigsForSecondLikelyMaleParentId;
		} else {
			related_pigsforsecondlikelymaleparentid = pigDAO.store(related_pigsforsecondlikelymaleparentid);
			pigDAO.flush();
		}

		pig.setPigByFemaleParentId(related_pigsforsecondlikelymaleparentid);
		related_pigsforsecondlikelymaleparentid.getPigsForFemaleParentId().add(pig);
		pig = pigDAO.store(pig);
		pigDAO.flush();

		related_pigsforsecondlikelymaleparentid = pigDAO.store(related_pigsforsecondlikelymaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Save an existing PigType entity
	 * 
	 */
	@Transactional
	public Pig savePigPigType(Integer id, PigType related_pigtype) {
		Pig pig = pigDAO.findPigByPrimaryKey(id, -1, -1);
		PigType existingpigType = pigTypeDAO.findPigTypeByPrimaryKey(related_pigtype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigType != null) {
			existingpigType.setId(related_pigtype.getId());
			existingpigType.setName(related_pigtype.getName());
			existingpigType.setMemo(related_pigtype.getMemo());
			related_pigtype = existingpigType;
		}

		pig.setPigType(related_pigtype);
		related_pigtype.getPigs().add(pig);
		pig = pigDAO.store(pig);
		pigDAO.flush();

		related_pigtype = pigTypeDAO.store(related_pigtype);
		pigTypeDAO.flush();

		return pig;
	}

	/**
	 * Save an existing PigWeight entity
	 * 
	 */
	@Transactional
	public Pig savePigPigWeights(Integer id, PigWeight related_pigweights) {
		Pig pig = pigDAO.findPigByPrimaryKey(id, -1, -1);
		PigWeight existingpigWeights = pigWeightDAO.findPigWeightByPrimaryKey(related_pigweights.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigWeights != null) {
			existingpigWeights.setId(related_pigweights.getId());
			existingpigWeights.setWeight(related_pigweights.getWeight());
			existingpigWeights.setTime(related_pigweights.getTime());
			existingpigWeights.setMemo(related_pigweights.getMemo());
			related_pigweights = existingpigWeights;
		}

		related_pigweights.setPig(pig);
		pig.getPigWeights().add(related_pigweights);
		related_pigweights = pigWeightDAO.store(related_pigweights);
		pigWeightDAO.flush();

		pig = pigDAO.store(pig);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig deletePigPigByFemaleParentId(Integer pig_id, Integer related_pigbyfemaleparentid_id) {
		Pig pig = pigDAO.findPigByPrimaryKey(pig_id, -1, -1);
		Pig related_pigbyfemaleparentid = pigDAO.findPigByPrimaryKey(related_pigbyfemaleparentid_id, -1, -1);

		pig.setPigByFemaleParentId(null);
		related_pigbyfemaleparentid.getPigsForFemaleParentId().remove(pig);
		pig = pigDAO.store(pig);
		pigDAO.flush();

		related_pigbyfemaleparentid = pigDAO.store(related_pigbyfemaleparentid);
		pigDAO.flush();

		pigDAO.remove(related_pigbyfemaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Save an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig savePigPigByMostLikelyMaleParentId(Integer id, Pig related_pigbymostlikelymaleparentid) {
		Pig pig = pigDAO.findPigByPrimaryKey(id, -1, -1);
		Pig existingpigByMostLikelyMaleParentId = pigDAO.findPigByPrimaryKey(related_pigbymostlikelymaleparentid.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigByMostLikelyMaleParentId != null) {
			existingpigByMostLikelyMaleParentId.setId(related_pigbymostlikelymaleparentid.getId());
			existingpigByMostLikelyMaleParentId.setNumber(related_pigbymostlikelymaleparentid.getNumber());
			existingpigByMostLikelyMaleParentId.setEarNumber(related_pigbymostlikelymaleparentid.getEarNumber());
			existingpigByMostLikelyMaleParentId.setRfid(related_pigbymostlikelymaleparentid.getRfid());
			existingpigByMostLikelyMaleParentId.setBirthday(related_pigbymostlikelymaleparentid.getBirthday());
			existingpigByMostLikelyMaleParentId.setState(related_pigbymostlikelymaleparentid.getState());
			existingpigByMostLikelyMaleParentId.setMemo(related_pigbymostlikelymaleparentid.getMemo());
			related_pigbymostlikelymaleparentid = existingpigByMostLikelyMaleParentId;
		} else {
			related_pigbymostlikelymaleparentid = pigDAO.store(related_pigbymostlikelymaleparentid);
			pigDAO.flush();
		}

		pig.setPigByMostLikelyMaleParentId(related_pigbymostlikelymaleparentid);
		related_pigbymostlikelymaleparentid.getPigsForMostLikelyMaleParentId().add(pig);
		pig = pigDAO.store(pig);
		pigDAO.flush();

		related_pigbymostlikelymaleparentid = pigDAO.store(related_pigbymostlikelymaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Save an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig savePigPigsForMostLikelyMaleParentId(Integer id, Pig related_pigsformostlikelymaleparentid) {
		Pig pig = pigDAO.findPigByPrimaryKey(id, -1, -1);
		Pig existingpigsForMostLikelyMaleParentId = pigDAO.findPigByPrimaryKey(related_pigsformostlikelymaleparentid.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigsForMostLikelyMaleParentId != null) {
			existingpigsForMostLikelyMaleParentId.setId(related_pigsformostlikelymaleparentid.getId());
			existingpigsForMostLikelyMaleParentId.setNumber(related_pigsformostlikelymaleparentid.getNumber());
			existingpigsForMostLikelyMaleParentId.setEarNumber(related_pigsformostlikelymaleparentid.getEarNumber());
			existingpigsForMostLikelyMaleParentId.setRfid(related_pigsformostlikelymaleparentid.getRfid());
			existingpigsForMostLikelyMaleParentId.setBirthday(related_pigsformostlikelymaleparentid.getBirthday());
			existingpigsForMostLikelyMaleParentId.setState(related_pigsformostlikelymaleparentid.getState());
			existingpigsForMostLikelyMaleParentId.setMemo(related_pigsformostlikelymaleparentid.getMemo());
			related_pigsformostlikelymaleparentid = existingpigsForMostLikelyMaleParentId;
		} else {
			related_pigsformostlikelymaleparentid = pigDAO.store(related_pigsformostlikelymaleparentid);
			pigDAO.flush();
		}

		pig.setPigByFemaleParentId(related_pigsformostlikelymaleparentid);
		related_pigsformostlikelymaleparentid.getPigsForFemaleParentId().add(pig);
		pig = pigDAO.store(pig);
		pigDAO.flush();

		related_pigsformostlikelymaleparentid = pigDAO.store(related_pigsformostlikelymaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig deletePigPigsForThirdLikelyMaleParentId(Integer pig_id, Integer related_pigsforthirdlikelymaleparentid_id) {
		Pig related_pigsforthirdlikelymaleparentid = pigDAO.findPigByPrimaryKey(related_pigsforthirdlikelymaleparentid_id, -1, -1);

		Pig pig = pigDAO.findPigByPrimaryKey(pig_id, -1, -1);

		pig.setPigByFemaleParentId(null);
		related_pigsforthirdlikelymaleparentid.getPigsForFemaleParentId().remove(pig);

		pigDAO.remove(related_pigsforthirdlikelymaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig deletePigPigBySecondLikelyMaleParentId(Integer pig_id, Integer related_pigbysecondlikelymaleparentid_id) {
		Pig pig = pigDAO.findPigByPrimaryKey(pig_id, -1, -1);
		Pig related_pigbysecondlikelymaleparentid = pigDAO.findPigByPrimaryKey(related_pigbysecondlikelymaleparentid_id, -1, -1);

		pig.setPigBySecondLikelyMaleParentId(null);
		related_pigbysecondlikelymaleparentid.getPigsForSecondLikelyMaleParentId().remove(pig);
		pig = pigDAO.store(pig);
		pigDAO.flush();

		related_pigbysecondlikelymaleparentid = pigDAO.store(related_pigbysecondlikelymaleparentid);
		pigDAO.flush();

		pigDAO.remove(related_pigbysecondlikelymaleparentid);
		pigDAO.flush();

		return pig;
	}

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@Transactional
	public Pig deletePigPigsForSecondLikelyMaleParentId(Integer pig_id, Integer related_pigsforsecondlikelymaleparentid_id) {
		Pig related_pigsforsecondlikelymaleparentid = pigDAO.findPigByPrimaryKey(related_pigsforsecondlikelymaleparentid_id, -1, -1);

		Pig pig = pigDAO.findPigByPrimaryKey(pig_id, -1, -1);

		pig.setPigByFemaleParentId(null);
		related_pigsforsecondlikelymaleparentid.getPigsForFemaleParentId().remove(pig);

		pigDAO.remove(related_pigsforsecondlikelymaleparentid);
		pigDAO.flush();

		return pig;
	}
}
